package com.wang.hello.qrcode.test;

import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.apache.struts2.StrutsTestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.context.WebApplicationContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;
import com.wang.hello.qrcode.QrCodeAction;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
	    "classpath:applicationContext-web-actions-selfserv.xml"
	})
//@TestExecutionListeners({
//	TransactionalTestExecutionListener.class,
//	DependencyInjectionTestExecutionListener.class,
//	DirtiesContextTestExecutionListener.class
//})
public class QrCodeActionTestCase extends StrutsTestCase implements ApplicationContextAware
{
	protected ApplicationContext applicationContext;
	
	public void setApplicationContext(final ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}
	
	@Before
	public void onSetUp() throws Exception {
		super.setUp();
	}
	
	protected void setupBeforeInitDispatcher() throws Exception {
		servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
	}

	@Test
	public void testGetQrCode()
	{
		request.setQueryString("code=qrcode_plain_text");
		request.setParameter("code", "qrcode_plain_text");
		
		ActionProxy proxy = getActionProxy("/qrcode/generate");
		ActionContext actionContext = proxy.getInvocation().getInvocationContext();
		actionContext.setSession(new HashMap<String, Object>());
		
		QrCodeAction qrCodeAction = (QrCodeAction) proxy.getAction();

		String result = qrCodeAction.execute();
		
		assertTrue("There should be no field errors: " + qrCodeAction.getFieldErrors(), qrCodeAction.getFieldErrors().size() == 0);
		assertTrue("There should be no action errors: " + qrCodeAction.getActionErrors(), qrCodeAction.getActionErrors().size() == 0);
		assertEquals("Result returned form executing the action was not success but it should have been.", Action.SUCCESS, result);
	}
}
